//
//  EditTransactionController.m
//  LuCaiZhu
//
//  Created by stella Ma on 2018/12/20.
//
#import "EditRenGouCell.h"
#import "CKPuslichTool.h"
#import "TKAlertCenter.h"
#import "ValidateTools.h"
#import "UIColor+NN.h"
#import "HandleNetProduct+Transaction.h"
#import "EditTransactionController.h"

@interface EditTransactionController (){
    NSString *bookingId;
    NSString *changeMoney;
}

@property (weak, nonatomic) IBOutlet UIButton *noteBt;
@property (weak, nonatomic) IBOutlet UITableView *mTableView;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSArray *contentArray;
@property (weak, nonatomic) IBOutlet UIButton *submitBt;
@end

@implementation EditTransactionController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIViewSetRadius(_submitBt, 22, 0, nil);
    self.mTableView.rowHeight = 55;
    if (_isEidt) {
        self.title = @"编辑预约单";
        
        bookingId = _orderMode.BookingId;
        
        NSString *moneySt = _orderMode.Amount;
        if ([moneySt rangeOfString:@","].length>0) {
            NSArray *aa = [moneySt componentsSeparatedByString:@","];
            moneySt = [aa componentsJoinedByString:@""];
        }
        changeMoney = moneySt;
        _contentArray = @[_orderMode.ProductName,_orderMode.PurchaseOpenDay,changeMoney];
    }else{
        self.title = @"新建预约单";
        _contentArray = @[_bookMode.ProductName,_bookMode.PurchaseOpenDay,@""];
        bookingId = _bookMode.BookingId;
        changeMoney = @"";
    }

    _dataArray = @[@"产品名称",@"开放日",@"认购金额(万)"];
    NSString *ammount;
    if (_isEidt) {
        ammount = _orderMode.ProgressiveAmount;
       
    }else {
        ammount = _bookMode.ProgressiveAmount;
    }
    [_noteBt setTitle:[NSString stringWithFormat:@"备注：追加认购金额最低为%@万元，认购金额不包含认购费。",ammount] forState:UIControlStateNormal];
        
    
}

- (IBAction)submit:(id)sender {
    _submitBt.enabled = NO;
    if (changeMoney.length == 0) {
        [[TKAlertCenter defaultCenter]postAlertWithMessage:@"请输入认购金额"];
        return;
    }
    //判断输入的是数字
    if (![ValidateTools validateMoney:changeMoney]) {
        [[TKAlertCenter defaultCenter]postAlertWithMessage:@"请输入数字"];
        return;
    }
    
    if (_isEidt) {
        if ([_orderMode.Amount isEqualToString:changeMoney]) {
            [[TKAlertCenter defaultCenter]postAlertWithMessage:@"请修改认购金额后提交"];
            return;
        }
        //编辑BookingEdit
        [HandleNetProduct EditOrAddBookingforMethodName:@"BookingEdit" BookingID:bookingId bookingAmount:changeMoney Success:^(NSDictionary * _Nonnull data) {

            _submitBt.enabled = YES;
            if (self.returnAddBlock) {
                self.returnAddBlock(data);
            }
            [self.navigationController popViewControllerAnimated:YES];

        } Failure:^(NSString * _Nonnull errorInfo) {

            _submitBt.enabled = YES;

        }];
    }else{
        //新建
        [HandleNetProduct EditOrAddBookingforMethodName:@"AddBooking" BookingID:bookingId bookingAmount:changeMoney Success:^(NSDictionary * _Nonnull data) {
            _submitBt.enabled = YES;
            if (self.returnAddBlock) {
                self.returnAddBlock(data);
            }
            [self.navigationController popViewControllerAnimated:YES];

        } Failure:^(NSString * _Nonnull errorInfo) {
            _submitBt.enabled = YES;
        }];
    }
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.dataArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 
    EditRenGouCell *cell=[tableView dequeueReusableCellWithIdentifier:@"EditRenGouCell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.accessoryType=UITableViewCellAccessoryNone;
    cell.leftLa.text = _dataArray[indexPath.row];
    cell.rightLa.text = _contentArray[indexPath.row];
    
    if (indexPath.row <2) {
        cell.rightLa.hidden = NO;
        cell.mTextField.hidden = YES;
        cell.rightLa.textColor = [UIColor colorWithHexString:@"676869"];
    }else{
        cell.rightLa.hidden = YES;
        cell.mTextField.hidden = NO;
        cell.mTextField.text = _contentArray[indexPath.row];
        if (_isEidt) {
            [cell.mTextField becomeFirstResponder];
        }
        
    }
    cell.returnBlock = ^(NSString * _Nonnull text) {
        
        changeMoney = text;
    };
    
    return cell;
 
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}


@end
